package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.AddToCart;
import com.cskaoyan.gateway.form.shopping.UpdateCart;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.dto.*;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("shopping")
public class CartController {

    @Reference(timeout = 3000, retries = 0, check = false)
    ICartService iCartService;

    /**
     * 更新购物车中商品的数量
     *
     * @param
     * @return
     */
    @PutMapping("carts")
    public ResponseData updateCarts(@RequestBody UpdateCart updateCart) {
        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(updateCart.getUserId());
        updateCartNumRequest.setChecked(updateCart.getChecked());
        updateCartNumRequest.setProductId(updateCart.getProductId());
        updateCartNumRequest.setProductNum(updateCart.getProductNum());
        UpdateCartNumResponse response = iCartService.updateCartNum(updateCartNumRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 删除购物车中的商品
     *
     * @param
     * @return
     */

    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCart(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);
        DeleteCartItemResponse response = iCartService.deleteCartItem(deleteCartItemRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 删除选中的商品
     *
     * @param
     * @return
     */
    @DeleteMapping("items/{uid}")
    public ResponseData deleteCartItem(@PathVariable("uid") Long uid) {

        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);
        DeleteCheckedItemResposne response = iCartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 选择购物车中的所有商品
     *
     * @param
     * @return
     */
    @PutMapping("items")
    public ResponseData chooseCart(@RequestBody CheckAllItemRequest checkAllItemRequest) {
        CheckAllItemResponse response = iCartService.checkAllCartItem(checkAllItemRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 查看购物车
     *
     * @param request
     * @return
     */
    @GetMapping("/carts")
    public ResponseData getCartsListById(HttpServletRequest request) {
        //获取用户id
        Long uid = (Long) request.getAttribute(TokenIntercepter.USER_INFO_KEY);

        //转换
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);
        //查询
        CartListByIdResponse response = iCartService.getCartListById(cartListByIdRequest);
        //返回数据
        if (SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 添加到购物车
     *
     * @param addToCart
     * @return
     */
    @PostMapping("/carts")
    public ResponseData addToCart(@RequestBody AddToCart addToCart) {
        //转换
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(Long.valueOf(addToCart.getUserId()));
        addCartRequest.setItemId((long) addToCart.getProductId());
        addCartRequest.setNum(addToCart.getProductNum());
        AddCartResponse response = iCartService.addToCart(addCartRequest);
        //返回数据
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(null);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
